package com.holyn.guofang.viewcommon.itemdecoration;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

/**
 * Created by Holyn on 2017/7/23.
 */

public class LinearLayoutItemDecoration  extends RecyclerView.ItemDecoration {

    private Drawable mDivider;
    private int mDividerWidth = 1;
    private int linearLayoutDirection = LinearLayoutManager.VERTICAL;

    private final int[] ATTRS = new int[]{android.R.attr.listDivider};

    public LinearLayoutItemDecoration(Context context) {
        TypedArray a = context.obtainStyledAttributes(ATTRS);
        this.mDivider = a.getDrawable(0);
        a.recycle();

    }

    public LinearLayoutItemDecoration(Context context, int linearLayoutDirection) {
        this.linearLayoutDirection = linearLayoutDirection;
        TypedArray a = context.obtainStyledAttributes(ATTRS);
        this.mDivider = a.getDrawable(0);
        a.recycle();

    }

    public LinearLayoutItemDecoration(Context context, int mDividerWidth, int linearLayoutDirection) {
        this.linearLayoutDirection = linearLayoutDirection;
        this.mDividerWidth = mDividerWidth;
        TypedArray a = context.obtainStyledAttributes(ATTRS);
        this.mDivider = a.getDrawable(0);
        a.recycle();
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDraw(c, parent, state);
        if (linearLayoutDirection == LinearLayoutManager.VERTICAL){
            drawHorizontalLine(c, parent, state);
        } else if (linearLayoutDirection == LinearLayoutManager.HORIZONTAL){
            drawVerticalLine(c, parent, state);
        }
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        if (linearLayoutDirection == LinearLayoutManager.VERTICAL){
            outRect.set(0, 0, 0, mDividerWidth);
        } else if (linearLayoutDirection == LinearLayoutManager.HORIZONTAL){
            outRect.set(0, 0, mDividerWidth, 0);
        }
    }

    //画横线, 这里的parent其实是显示在屏幕显示的这部分
    public void drawHorizontalLine(Canvas c, RecyclerView parent, RecyclerView.State state){
        int left = parent.getPaddingLeft();
        int right = parent.getWidth() - parent.getPaddingRight();
        final int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++){
            final View child = parent.getChildAt(i);

            //获得child的布局信息
            final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams)child.getLayoutParams();
            final int top = child.getBottom() + params.bottomMargin;
            final int bottom = top + mDivider.getIntrinsicHeight();
            mDivider.setBounds(left, top, right, bottom);
            mDivider.draw(c);
        }
    }

    //画竖线
    public void drawVerticalLine(Canvas c, RecyclerView parent, RecyclerView.State state){
        int top = parent.getPaddingTop() ;
        int bottom = parent.getHeight() - parent.getPaddingBottom();
        final int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++){
            final View child = parent.getChildAt(i);

            //获得child的布局信息
            final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams)child.getLayoutParams();
            final int left = child.getRight() + params.rightMargin;
            final int right = left + mDividerWidth;
            mDivider.setBounds(left, top, right, bottom);
            mDivider.draw(c);
        }
    }
}
